<template>
  <el-container>
    <el-header>
      <div class="report_header" ref="headerHeight">
        <div class="report_header_left"></div>
        <div style="display: flex;flex-direction: row;justify-content:space-between;width: 100%;">
          <div style="display: flex;flex-direction: row;flex-wrap: wrap;overflow: hidden;height:41px;justify-content: flex-end;">
            <div class="report_header_date">
              <el-date-picker
              style="width: 100%;"
                v-model="query.date"
                value-format="yyyy-MM-dd"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions">
              </el-date-picker>
            </div>
            <div class="report_header_query">
              <el-select v-model="memberName" clearable placeholder="客户"
                         filterable @focus="getMemberList('')" :filter-method="queryMember" @change="selectMember">
                <el-option v-for="item in members" style="width: 600px;"  :key="item.id" :label="item.name"
                           :value="item">
                  <div style="display: flex">
                    <span style="width: 60px;">{{item.code}}</span>
                    <span style="float: left;width: 100px;overflow: hidden;text-overflow: ellipsis;">{{ item.name }}</span>
                    <span style="float: right; color: #8492a6; font-size: 8px;width: 80px;margin-left: 10px">{{ item.mobile }}</span>
                    <div style="width: 200px;">
                      <span style="color: #8492a6; font-size: 8px;">{{item.address}}</span>
                    </div>
                  </div>
                </el-option>
                <div style="display: flex;width: 100%;position: absolute;bottom: 0">
                  <div style="width: 50%;background-color: rgb(248,248,248);"></div>
                  <div class="elOption" @click="toSearchMember">搜索客户</div>
                </div>
                <div style="height: 30px"></div>
              </el-select>
            </div>
            <div class="report_header_query" style="width:150px;">
              <el-select v-model="paymentPluginId" clearable placeholder="付款方式">
                <el-option label="微信" value="weixinPayPlugin"></el-option>
                <el-option label="支付宝" value="alipayPayPlugin"></el-option>
                <el-option label="现金" value="cashPayPlugin"></el-option>
                <el-option label="转账" value="bankPayPlugin"></el-option>
                <el-option label="电子水票" value="waterTicketPayPlugin"></el-option>
                <el-option label="纸质水票" value="paperPayPlugin"></el-option>
                <el-option label="会员卡" value="cardPayPlugin"></el-option>
                <el-option label="红包" value="pointPayPlugin"></el-option>
                <el-option label="购物券" value="matchPointPayPlugin"></el-option>
                <el-option label="兑换券" value="groupPayPlugin"></el-option>
                <!--<el-option label="云库存" value="balancePayPlugin"></el-option>-->
              </el-select>
            </div>
            <div class="report_header_query">
              <el-input v-model="query.orderSn" placeholder="订单号"></el-input>
            </div>

          </div>
          <div>
          <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="queryPayment">查询</el-button>
          <ExportList2Excel style='margin-left: 10px;margin-top: 5px;' ref="exportList2Excel" @handleExport="handleExport" name="导出" />
          </div>
        </div>
      </div>
    </el-header>
    <div class="pd10">
      <el-tabs type="border-card" @tab-click="tabsClick" :style="{height:curHeight+'px'}">
        <el-tab-pane label="全部">
          <div class="details_card" v-loadMore="[payLoads,'refunds-list1']" :style="{height:tabsHeight+'px'}">
            <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
          </div>
        </el-tab-pane>
        <el-tab-pane label="等待退款">
          <div class="details_card" v-loadMore="[payLoads,'refunds-list2']" :style="{height:tabsHeight+'px'}">
            <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
          </div>
        </el-tab-pane>
        <el-tab-pane label="确认退款">
          <div class="details_card" v-loadMore="[payLoads,'refunds-list3']" :style="{height:tabsHeight+'px'}">
            <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
          </div>
        </el-tab-pane>
        <el-tab-pane label="退款完成">
          <div class="details_card" v-loadMore="[payLoads,'refunds-list4']" :style="{height:tabsHeight+'px'}">
            <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
          </div>
        </el-tab-pane>
        <el-tab-pane label="退款失败">
          <div class="details_card" v-loadMore="[payLoads,'refunds-list5']" :style="{height:tabsHeight+'px'}">
            <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
          </div>
        </el-tab-pane>
        <el-tab-pane label="取消退款">
          <div class="details_card" v-loadMore="[payLoads,'refunds-list6']" :style="{height:tabsHeight+'px'}">
            <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-footer class="el-footerBox">
      <pagination  :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                   @pagination="loadPayment"></pagination>
    </el-footer>

    <el-dialog
      title="双击选择客户"
      :visible.sync="memberTableVisible"
      width="950px"
      v-el-drag-dialog
      append-to-body
      :before-close="memberTableClose">
      <div style="position: relative;">
        <member-table :key="times" @memberDouble="memberDouble" @memberTableClose="memberTableClose"></member-table>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
  import {EventBus} from "@/api/eventBus";
  import loadMore from '@/utils/more.js'
  import paymentList from './components/list'
  import Pagination from '@/components/Pagination';
  import { list } from '../../api/refunds'
  import {list as memberList} from '@/api/member'
  import utils from '../../utils/utils.js'
  import ExportList2Excel from "@/components/ExportList2Excel";
  import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
  export default {
    name: 'refunds-list',
    directives: {loadMore},
    activated() {
      this.payLoads = new Date().getTime();
    },
    data(){
      return{
        payLoads:'',
        times:'',
        queryType: true,
        memberName:'',
        paymentPluginId: '',
        queryType: false,
        curHeight: null,
        tabsHeight:null,
        orderStatus:'',
        printing: 5000,
        members:[],
        total:0,
        query: {
          date: [],
          orderSn:'',
          title: '',
          memberId: '',
        },
        pageNum:1,
        pageSize:20,
        keyword: {
          date: [],
          title: '',
          orderSn:'',
          memberId: '',
        },
        payments:[],
        memberTableVisible:false,
        pickerOptions: {//时间快捷选择
          shortcuts: [{
            text: '昨天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      }
    },
    components:{
      Pagination,paymentList,ExportList2Excel
    },
    created() {
      EventBus.$on("refunds-details", res => {
        for (var i = 0; i < this.payments.length; i++) {
          if (this.payments[i].id == res.id) {
            this.$set(this.payments, i, res)
          }
        }

      });
      this.loadPayment()
    },
    mounted(){
      this.comeIn();
      this.beforeMount()
    },
    methods:{
      memberDouble(data){
        this.memberName=data.name
        this.query.memberId=data.id
        this.memberTableVisible = false
      },
      memberTableClose(){
        this.memberTableVisible = false
      },
      selectMember(data){
        if(!utils.isNull(data)){
          this.memberName=data.name
          this.query.memberId=data.id
        }else{
          this.query.memberId=''
        }

      },
      toSearchMember() {
        this.times=new Date().getSeconds()
        this.memberTableVisible = true
      },
      queryMember(query) {
        this.getMemberList(query)
      },
      getMemberList(query) {
        let param = new URLSearchParams()
        param.append('keyword', query)
        memberList(param).then(res => {
          this.members = res.data.data
        })
      },
      //下载模板的函数
      handleExport() {
        let _this= this
        var  loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        let data ={}
        data.tHeader = ["退款单号","订单号",'退款日期',"退款人","手机号","退款方式","摘要","退款金额"]
        data.filterVal = ["sn","orderSn",'refundsDate',"name","mobile","paymentPluginName","memo","amount"]
        data.filename = "收款单明细表";
        let params = new URLSearchParams();
        params.append('pageNum', this.pageNum)
        params.append('pageSize', this.printing)
        params.append('status',this.orderStatus);
        params.append('orderSn',this.keyword.orderSn)
        params.append('memberId', this.keyword.memberId);
        params.append('paymentPluginId',this.paymentPluginId);
        params.append('startDate',this.keyword.date.length >0?this.keyword.date[0]:'');
        params.append('endDate', this.keyword.date.length >0?this.keyword.date[1]:'');
        list(params).then(res => {
          data.list=res.data.data
          data.list.forEach(item=>{
            item.paymentDate = utils.dateFormatter(item.paymentDate)

          })
          _this.$refs.exportList2Excel.handleDownload(data);
          loading.close();
          if(data.list.length==this.printing){
            _this.pageNum++
            _this.handleExport()
          }
          else{
            return;
          }

        }).catch(error=>{
          _this.$refs.exportList2Excel.unlockLoading()
          loading.close();
        })
      },
      resetClick(){
        this.query.date = [];
        this.query.orderSn=''
        this.query.title = '';
        this.query.memberId = "";
        this.paymentPluginId = '';
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadPayment()
      },
      queryPayment(){
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadPayment()
      },
      detailsClick(data){
        this.$router.push({
          path:'/refund/view',query:{id:data}
        })
      },
      loadPayment(){
        if(utils.isNull(this.keyword.date)){
          this.keyword.date = []
        }
        let params = new URLSearchParams();
        params.append('pageNum', this.pageNum);
        params.append('pageSize', this.pageSize);
        params.append('status',this.orderStatus);
        params.append('orderSn',this.keyword.orderSn)
        params.append('memberId', this.keyword.memberId);
        params.append('paymentPluginId',this.paymentPluginId);
        params.append('startDate',this.keyword.date.length >0?this.keyword.date[0]:'');
        params.append('endDate', this.keyword.date.length >0?this.keyword.date[1]:'');
        list(params).then(response=>{
          this.total=response.data.total
          this.payments=response.data.data
        })
      },
      beforeMount() {
        var _this = this
        window.onresize = () => {
          return (() => {
            if (_this.queryType == false) {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 270//减去页面上固定高度height
              _this.tabsHeight = h - 381
            } else {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 224//减去页面上固定高度height
              this.tabsHeight = h - 294
            }
          })();
        };
      },
      comeIn() {
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 224//减去页面上固定高度height
        this.tabsHeight = h - 294
      },
      chooseClick() {
        this.queryType = !this.queryType
        if (this.queryType == false) {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 270//减去页面上固定高度height
          this.tabsHeight = h - 381
        } else {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 224//减去页面上固定高度height
          this.tabsHeight = h - 294
        }
      },
      tabsClick(data) {
        var _this = this
        console.log(data)
        switch (data.paneName) {
          case '0':
            _this.orderStatus = '' // 订单状态
            break;
          case '1':
            _this.orderStatus = 0 // 订单状态
            break;
          case '2':
            _this.orderStatus = 1 // 订单状态
            break;
          case '3':
            _this.orderStatus = 2 // 订单状态
            break;
          case '4':
            _this.orderStatus = 3 // 订单状态
            break;
          case '5':
            _this.orderStatus = 4 // 订单状态
            break;
        }
        this.loadPayment()
      }
    }

  }
</script>

<style scoped>
  .details_card {
    overflow: auto;
    padding-left: 20px;
    padding-right: 20px;
    box-sizing: border-box;
    border: 1px solid #f2f2f2;
    border-radius: 4px;
  }

  .details_card:hover {
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
  }

  .app {
    background-color: rgba(248, 248, 248, 0.8);
  }

  /deep/.el-header {
    padding: 10px;
    /* height: 100% !important; */
    padding-bottom: 0;
  }

  .report_header {
    width: 100%;
    min-height: 40px;
    box-sizing: border-box;
    background-color: rgba(248, 248, 248, 0.8);
    display: flex;
    align-items: center;
    padding-bottom: 5px;
    position: relative;
    padding-left: 5px;
    padding-right: 5px;
  }
  .report_header_left {
    width: 5px;
    height: 100%;
    background-color: #409EFF;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
  }

  .report_header_query {
    margin-left: 10px;
    width: 180px;
    margin-top: 5px;
  }
  .report_header_date {
      margin-left: 10px;
      width: 250px;
      margin-top: 5px;
    }
</style>
